/*
* @Author: sam
* @Date:   2017-10-11 14:02:11
* @Last Modified by:   sam
* @Last Modified time: 2017-10-30 14:21:03
*/
var listBox;
var params,code,pageSize = 10,pageNum = 1,vname = "";
(function(win){
	listBox = new Class.ListBox({
		id : "listBox",
		PullUpFun : PullUpFun,
		PullDownFun : PullDownFun,
		searchFun : searchFun
	});

	params = Utils.getParams();
	code = params.code;
	//

	// var data = [{
	// 	'areaName' : '湘潭市韶山市韶山村',
	// 	'star' : 5,
	// 	'detailUrl' : './detail.html',
	// 	'img' : 'images/1.jpg',
	// 	'contactName' : '庞立民',
	// 	'contactTel' : '13873203687',
	// 	'address' : '湘潭市韶山市韶山村',
	// 	'lng' : 112.48065,
	// 	'lat' : 27.90337
	// },
	// {
	// 	'areaName' : '湘潭市韶山市韶山县',
	// 	'star' : 4,
	// 	'detailUrl' : './detail.html',
	// 	'img' : 'images/1.jpg',
	// 	'contactName' : '庞立民2',
	// 	'contactTel' : '13800000000',
	// 	'address' : '湘潭市韶山市韶山县',
	// 	'lng' : 112.48065,
	// 	'lat' : 27.90337
	// }];
	// listBox.rendHtml(data);
	getList();


})(window);

function getList(){
	Utils.ajax({
		type : 'get',
		url : Configs.api.getDetailList,
		data : { code : code, pageSize : pageSize, pageNum : pageNum, name : vname},
		beforeSend : function(){
			//loading.classList.add('show');
		},
		afterSend : function(){
			//loading.classList.remove('show');
		},
		success : function(json){
			var data = JSON.parse(json);
			var obj = {},list,obj,arr = [];
			var townName;
			if(data.status === Configs.status.success){
				list = data.data;
				if(list.length > 0){
					for(var i = 0; i < list.length; i++){
						obj = {};
						obj['areaName'] = list[i]['Name'];
						obj['star'] = list[i]['Level'];
						obj['detailUrl'] = './detail.html?code=' + list[i]['Code'];
						obj['img'] = list[i]['Img'];
						obj['contactName'] = list[i]['ManagerName'];
						obj['contactTel'] = list[i]['ManagerMobile'];
						obj['address'] = list[i]['NameDetail'];
						obj['lng'] = list[i]['Longtude'];
						obj['lat'] = list[i]['Latitude'];
						townName = list[i]['TownName'] + "下属村";
						arr.push(obj);
					}
					listBox.rendHtml(arr);
					pageNum++;
				}else{
					townName = "暂无数据";
					listBox.changePullUp(false);
				}
				Utils.setTitle(townName);
			}

			if(data.status === Configs.status.empty){
				listBox.changePullUp(false);
				//console.log('暂无数据');
			}
		}
	});
}

//打电话
function makeCall(dom){
	Utils.makeCall(dom);
}

//打开app
function openApp(dom){
	var openTypeBox = document.querySelector('.openTypeBox');
	var lng = dom.getAttribute('lng');
	var lat = dom.getAttribute('lat');
	var dest_name = dom.getAttribute('dest_name');
	var dest = {
		'lng' : lng,
		'lat' : lat,
		'dest_name' : dest_name
	};
	var ddDom;
	if(!openTypeBox.classList.contains('bok')){
		openTypeBox.classList.add('bok');
		openTypeBox.style.height = (listBox.dom.offsetHeight + 65) + 'px';
		window.setTimeout(function(){
			openTypeBox.classList.add('show');
			ddDom = document.querySelectorAll('.openType dd');
			for(var i = 0; i < ddDom.length; i++){
			 	(function(n){
			 		if(ddDom[n].onclick === null){
				 		ddDom[n].onclick = function(){
				 			listBox.changeMapType(this.getAttribute('type'));
				 			if(openTypeBox.classList.contains('bok')){
				 				openTypeBox.classList.remove('show');
				 				window.setTimeout(function(){
				 					openTypeBox.classList.remove('bok');
				 				},200)
				 			}
				 			listBox.openMapApp(dest);
				 			console.log(listBox);
				 		};
			 		}

			 	})(i);
			}
			openTypeBox.onclick = function(){
	 			if(openTypeBox.classList.contains('bok')){
	 				openTypeBox.classList.remove('show');
	 				window.setTimeout(function(){
	 					openTypeBox.classList.remove('bok');
	 				},200)
	 			}
			};
		},0);
	}

}

//上拉加载
function PullUpFun(){
	var input = document.getElementById('listSearch');
	vname = input.value;
	getList();
}

//下拉刷新
function PullDownFun(){
	var input = document.getElementById('listSearch');
	vname = input.value;
	pageNum = 1;
	getList();
}

//输入关键字查找
function searchFun(){
	var input = document.getElementById('listSearch');
	vname = input.value;
	PullDownFun();
}